app.controller('courseController', function ($scope, $controller, courseService) {

    $controller('baseController', {$scope: $scope});//继承


    //读取列表数据绑定到表单中
    $scope.findAll = function () {
        courseService.findAll().success(
            function (response) {
                $scope.list = response;
            }
        );
    }


    //根据id查询课程
    $scope.findOne = function (id) {
        courseService.findOne(id).success(
            function (response) {
                $scope.entity = response;
            }
        );
    }

    //保存
    $scope.save = function () {
        var serviceObject;//服务层对象
        if ($scope.entity.id != null) {//如果有ID
            serviceObject = courseService.update($scope.entity); //修改
        } else {
            serviceObject = courseService.add($scope.entity);//增加
        }
        serviceObject.success(
            function (response) {
                if (response.success) {
                    alert(response.message);
                    window.location = "/admin/goods.html";
                } else {
                    alert(response.message);
                }
            }
        );
    }

    //批量删除
    $scope.dele = function () {

        //判断是否选中选项
        if ($scope.selectIds != null && $scope.selectIds.length > 0) {
            if (confirm("确认删除？")) {
                //获取选中的复选框
                courseService.dele($scope.selectIds).success(
                    function (response) {
                        if (response.success) {
                            $scope.findAll();//刷新列表
                            $scope.selectIds = [];
                        }
                    }
                );
            }
        } else {
            alert("请先选择需要删除的数据！");
        }

    }

    //全局条件查询
    $scope.search = function () {
        courseService.search($scope.searchEntity).success(
            function (response) {
                $scope.list = response;
            }
        );
    }
});